package com.ztgf.order.api.client.purchase;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageSaveBO;
import com.ztgf.order.models.vo.purchase.PurchaseCommodityLeaveMessageAndReplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityLeaveMessageService;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageBO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityLeaveMessageDTO;

import java.util.List;

/**
 * 询价留言表控制器
 *
 * @author mxli
 * @Date 2021-08-24 15:42:41
 */

@RestController
@RequestMapping("/api/purchaseCommodityLeaveMessage")
@Slf4j
@Api(value = "PurchaseCommodityLeaveMessageApiController",tags = "询价留言表Api接口")
public class PurchaseCommodityLeaveMessageApiController {

    @DubboReference
    private IPurchaseCommodityLeaveMessageService purchaseCommodityLeaveMessageService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseCommodityLeaveMessageDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        PurchaseCommodityLeaveMessageDTO dto = purchaseCommodityLeaveMessageService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "询价留言表列表",notes = "询价留言表列表")
    @PostMapping("/queryPageList")
    public Result<Page<List<PurchaseCommodityLeaveMessageAndReplyVO>>> queryPageList(PurchaseCommodityLeaveMessageBO bo){
        /*Wrapper<PurchaseCommodityLeaveMessageDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<PurchaseCommodityLeaveMessageDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = purchaseCommodityLeaveMessageService.selectPage(page, wrapper);*/
        Page<List<PurchaseCommodityLeaveMessageAndReplyVO>> page=  purchaseCommodityLeaveMessageService.queryByApiPageList(bo);

        return new Result<>(page);
    }


    @ApiOperation(value = "添加询价留言表",notes = "添加询价留言表")
    @PostMapping(value = "/insert")
    public Result insert(PurchaseCommodityLeaveMessageSaveBO saveBO) {
        boolean flag = purchaseCommodityLeaveMessageService.addByApi(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @ApiOperation(value = "屏蔽询价留言表",notes = "屏蔽询价留言表")
    @ApiImplicitParam(name = "id",value = "询价留言表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        PurchaseCommodityLeaveMessageDTO purchaseCommodityLeaveMessageDTO = purchaseCommodityLeaveMessageService.selectById(id);
        if (purchaseCommodityLeaveMessageDTO!=null){
            purchaseCommodityLeaveMessageDTO.setStatus(1);
            boolean flag = purchaseCommodityLeaveMessageService.updateById(purchaseCommodityLeaveMessageDTO);
            if (flag){
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL_100009);
            }
        }
      return new Result<>();
    }




    @ApiOperation(value = "取消屏蔽留言",notes = "取消屏蔽留言")
    @PostMapping("/updateById")
    public Result update(@RequestParam("id") Long id) {
        PurchaseCommodityLeaveMessageDTO purchaseCommodityLeaveMessageDTO = purchaseCommodityLeaveMessageService.selectById(id);
        if (purchaseCommodityLeaveMessageDTO != null) {
            purchaseCommodityLeaveMessageDTO.setStatus(0);
            boolean flag = purchaseCommodityLeaveMessageService.updateById(purchaseCommodityLeaveMessageDTO);
            if (flag) {
                return new Result<>();
            } else {
                return new Result<>(CodeEnum.FAIL_100008);
            }
        }
        return new Result<>();
    }
}
